﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UCL.Forms.Components
{
    /// <summary>
    /// 下拉框编辑器
    /// <para>如：</para>
    /// <para>Dictionary&lt;string, string&gt; dictCBBE = new Dictionary&lt;string, string&gt;();</para>
    /// <para>private void configComboboxEditor()</para>
    /// <para>{</para>
    /// <para>    dictCBBE.Add("主动", "5.0");</para>
    /// <para>    dictCBBE.Add("IVR","10.0");</para>
    /// <para>    dictCBBE.Add("IVRCon","3.0");</para>
    /// <para>    comboBoxEditor1.TextChangedHandler = (string name, string value) =&gt;</para>
    /// <para>    {</para>
    /// <para>        dictCBBE[name] = value;</para>
    /// <para>    };</para>
    /// <para>    comboBoxEditor1.SaveDataHandler = (string name, string value) =&gt;</para>
    /// <para>    {</para>
    /// <para>        if (value.Length == 0)</para>
    /// <para>        {</para>
    /// <para>            MessageBox.Show("值无效");</para>
    /// <para>            return;</para>
    /// <para>        }</para>
    /// <para>        dictCBBE[name] = value;</para>
    /// <para>        MessageBox.Show("设置成功");</para>
    /// <para>    };</para>
    /// <para>    comboBoxEditor1.SelectedIndexChangedHandler = (string name) =&gt;</para>
    /// <para>    {</para>
    /// <para>        return dictCBBE[name];</para>
    /// <para>    };</para>
    /// <para>    comboBoxEditor1.Names = dictCBBE.Keys.ToArray();</para>
    /// <para>}</para>
    /// </summary>
    /// <remarks>2014-3-7 by sutroon</remarks>
    public partial class ComboBoxEditor : UserControl
    {
        /// <summary>
        /// 获取或设置下拉框控件的宽度
        /// </summary>
        public int ComboboxWidth { get { return cbbName.Width; } set { cbbName.Width = value; ResizeControlWidth(); } }

        /// <summary>
        /// 获取或设置文本框的宽度
        /// </summary>
        public int TextBoxWidth { get { return tbValue.Width; } set { tbValue.Width = value; ResizeControlWidth(); } }

        /// <summary>
        /// 获取或设置按钮的可见状态
        /// </summary>
        public bool ShowButton { get { return btnSave.Visible; } set { btnSave.Visible = value; ResizeControlWidth(); } }

        /// <summary>
        /// 获取或设置按钮的文本
        /// </summary>
        public string ButtonText { get { return btnSave.Text; } set { btnSave.Text = value; ResizeControlWidth(); } }

        /// <summary>
        /// 获取或设置文本框值改变的时候的委托方法
        /// </summary>
        public Action<string, string> TextChangedHandler { get; set; }

        /// <summary>
        /// 获取或设置保存数据的委托方法
        /// </summary>
        public Action<string, string> SaveDataHandler { get; set; }

        /// <summary>
        /// 获取或设置下拉框值改变的时候的委托方法
        /// </summary>
        public Func<string, string> SelectedIndexChangedHandler { get; set; }

        /// <summary>
        /// 获取或设置下拉框的值
        /// </summary>
        public string[] Names { get { return cbbName.DataSource == null ? null : (string[])cbbName.DataSource; } set { cbbName.DataSource = value; } }

        /// <summary>
        /// 获取或设置文本框的值
        /// </summary>
        public string Value { get { return tbValue.Text; } set { tbValue.Text = value; } }

        public ComboBoxEditor()
        {   
            InitializeComponent();
            btnSave.Click += new EventHandler(btnSave_Click);
            cbbName.SelectedIndexChanged += new EventHandler(cbbName_SelectedIndexChanged);
            tbValue.TextChanged += new EventHandler(tbValue_TextChanged);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (SaveDataHandler != null) SaveDataHandler(GetSelectedValue(cbbName), tbValue.Text);
        }
        private void cbbName_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (SelectedIndexChangedHandler != null) tbValue.Text = SelectedIndexChangedHandler(GetSelectedValue(cbbName));
        }
        private void tbValue_TextChanged(object sender, EventArgs e)
        {
            if (TextChangedHandler != null) TextChangedHandler(GetSelectedValue(cbbName), tbValue.Text);
        }

        int width;
        // 重新设置控件的宽度
        private void ResizeControlWidth()
        {
            width = cbbName.Width;
            width += tbValue.Width;
            if (btnSave.Visible) width += btnSave.Width;
            this.Width = width;
        }

        /// <summary>
        /// 获取选中的值
        /// </summary>
        /// <param name="c"></param>
        /// <param name="valIndex"></param>
        /// <returns></returns>
        private string GetSelectedValue(ListControl c, int valIndex = 0)
        {
            object item = c.SelectedValue;
            if (item == null) return c.Text.Length > 0 ? c.Text : string.Empty;
            if (item.GetType() == typeof(DataRowView) || item.GetType() == typeof(DataRow))
            {
                item = ((DataRowView)item)[valIndex];
                return item == null ? string.Empty : item.ToString();
            }
            return item.ToString();
        }
    }
}
